// ~ CopyRight © 2012 China Mobile Group Anhui CO.,LTD All Rights Reserved.
package com.cmcc.zysoft.infocollection.mobile.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmcc.zysoft.framework.utils.UUIDUtil;
import com.cmcc.zysoft.infocollection.aop.LoginLogAspect;
import com.cmcc.zysoft.infocollection.common.BaseController;
import com.cmcc.zysoft.infocollection.mobile.service.SystemUserService;
import com.cmcc.zysoft.infocollection.model.Company;
import com.cmcc.zysoft.infocollection.model.SystemUser;
import com.cmcc.zysoft.infocollection.util.MD5Tools;
import com.cmcc.zysoft.infocollection.util.RandPass;
//import com.cmcc.zysoft.infocollection.webservice.client.ADCClient;
import com.cmcc.zysoft.spring.security.util.SecurityContextUtil;
import com.cmcc.zysoft.sysmanage.service.CompanyService;

/**
 * @author yan.dou@ustcinfo.com
 * @2013-7-10 下午4:17:31
 * @SystemUserController.java
 */
@Controller
@RequestMapping("/mobileSale/userMgr")
public class SystemUserController extends BaseController {

	/**
	 * 属性名称：systemUserService <br/>
	 * . . 类型：SystemUserService
	 */
	@Resource
	private SystemUserService systemUserService;

	/**
	 * 属性名称：companyService <br/>
	 * . 类型：CompanyService
	 */
	@Resource
	private CompanyService companyService;

	/**
	 * 属性名称：adcClient <br/>
	 * . 类型：ADCClient
	 */
//	@Resource
//	private ADCClient adcClient;

	/**
	 * 手机侧登录验证接口. http://localhost:8282/mobileSale/userMgr/loginPhone?userCode=
	 * 18756068416 &userPass=111111
	 * 
	 * @param userCode
	 *            用户名
	 * @param userPass
	 *            密码
	 * @param request
	 *            http请求对象
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "/loginPhone")
	@ResponseBody
	@Transactional
	public Map<String, Object> loginPhone(String userCode, String userPass,
			HttpServletRequest request) {
		// 验证登录信息之前记录登录开始时间点，以便于计算登录方法耗时
		LoginLogAspect.TIME_COMSUME_MAP.put(userCode,
				System.currentTimeMillis());
		Map<String, Object> map = this.systemUserService.loginPhone(userCode,
				userPass);
		String IP = SecurityContextUtil.getRemoteIP(request);
		map.put("loginIp", IP);
		return map;
	}

	/**
	 * 手机侧修改密码.
	 * 
	 * @param userCode
	 *            用户登录账户
	 * @param oldPassword
	 *            旧密码
	 * @param newPassword
	 *            新密码
	 * @param companyId
	 *            公司Id
	 * @param request
	 *            http请求对象
	 * @return Map<String, String>
	 */
	@RequestMapping(value = "/changePassWord")
	@Transactional
	@ResponseBody
	public Map<String, String> changePassWord(String userCode,
			String oldPassword, String newPassword, String companyId,
			HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		String userId = this.getUserIdByName(userCode);
		SystemUser user = this.systemUserService.getEntity(userId);

		if (!MD5Tools.isPasswordValid(user.getPassword(), oldPassword,
				user.getPassSalt())) {
			map.put("success", "false");
			map.put("msg", "旧密码输入不正确");
			return map;
		}
		String passwordSalt = UUIDUtil.generateUUID();
		user.setPassSalt(passwordSalt);
		user.setPassword(MD5Tools.encodePassword(newPassword, passwordSalt));
		user.setModifyTime(new Date());
		this.systemUserService.updateEntity(user);
		map.put("success", "true");
		Company comp = companyService.getEntity(companyId);
//		adcClient.modifyPassword(comp.getCompanyCode(), userCode, oldPassword,
//				newPassword, SecurityContextUtil.getRemoteIP(request));
		return map;
	}

	/**
	 * 通过用户名获取用户Id(userId).
	 * 
	 * @param userName
	 *            登录账户
	 * @return 用户名
	 */
	public String getUserIdByName(String userName) {
		List<SystemUser> list = this.systemUserService.findByNamedParam(
				"userName", userName);
		if (list.size() > 0) {
			return list.get(0).getUserId().toString();
		} else {
			return "";
		}
	}

	/**
	 * 
	 * @param mobile
	 *            重置密码的手机号
	 * @return map
	 */
		@RequestMapping(value = "/resetPassWord")
		@Transactional
		@ResponseBody
	public Map<String, String> resetPass(String mobile) {
		Map<String, String> map = new HashMap<String, String>();
		String userId = this.getUserIdByName(mobile);
		if (userId.isEmpty()) {//校验账户是否开通了此项业务
			map.put("success", "false");
			map.put("msg", "您的号码未订购业务，请联系您单位管理员");
			return map;
		}
		SystemUser user = this.systemUserService.getEntity(userId);
		if (("1").equals(user.getDelFlag())) {//校验账户是否被停用
			map.put("success", "false");
			map.put("msg", "您的帐号已停用或未启用，请联系您单位管理员");
			return map;
		}
		String newPassword = RandPass.genRandomNum(6); // 随机生成6位长度的密码
		String passwordSalt = UUIDUtil.generateUUID();
		user.setPassSalt(passwordSalt);
		user.setPassword(MD5Tools.encodePassword(newPassword, passwordSalt));
		user.setModifyTime(new Date());
		this.systemUserService.updateEntity(user);
		map.put("success", "true");
		map.put("msg", newPassword);
		return map;

	}
}
